gdb 调试

调试

在 gdb 调试时,如果携带非常长的参数需要进行许多操作时,这个时候如果手动输入就非常麻烦,非常没有效率。

好在,求助万能的 man,可以看到其有一个参数是 -x, 在参数后可以跟一个文件,在这个文件中,我们可以连接设备,设置断点,查看内存等,与在 gdb 中操作一致。

man gdb 
...
       --command=file
       -x file
           Execute GDB commands from file.

示例:连接远程主机 qnx, 上传本地 main 到 /tmp/main 设置信号不拦截,在 crash_handler_more 处打断点,之后 r 是运行,如果要传入参数,则直接在 r 后面跟参数,比如 r 5 等于在终端中执行 ./main 5

# gdbinit.sh
target qnx 192.168.1.100:8000
file main 
upload main /tmp/main

handle SIGSEGV nostop
# handle SIGKILL nostop
# handle SIGABRT nostop

b crash_handler_more
# r 5

一般情况下运行时只需要 gdb -x gdbinit.sh,但是我这边比较特殊,使用的是 qnx 平台专门的 gdb 工具,所以首先需要导入环境变量,在执行 gdb

source /opt/qos20/qnxsdp-env.sh
ntoaarch64-gdb -x ./gdbinit.sh

之后就可以 gdb 调试了。

info register 可以显示寄存器信息

info frame 显示当前栈帧信息

info ... 等

还有在 gdb 中反汇编代码

disassembly 函数名

还有 layout asm 同时显示汇编与 c 语言代码

打印结构体,比如有如下一结构体,在进入 test 后,怎么打印传入的该值呢?

struct sample {
	int a,b,c,d,e,f;
	char array[100];
	uint8_t magic;
};

struct sample s;

void test(struct sample *s) {
	
}

参考

  1. GDB 命令脚本的编写以及调试技巧汇总
  2. GDB 信号处理 Signals Handle
  3. GDB调试之定位段错误
  4. gdb 打印 数组 16进制